package com.lang.dao.impl;

import com.lang.dao.CartDao;
import com.lang.entity.Cart;
import com.lang.entity.Product;
import com.lang.utils.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * @Date : 2024/3/13 16:12
 * @Created by Lang
 * @ProjectName : LclShop
 */
public class CartDaoImpl implements CartDao {
    private QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());

    @Override
    public Cart getCartByUidAndPid(int uId, String pid) {
        String sql = "select * from cart where u_id = ? and p_id = ?";

        try {
            return runner.query(sql, new BeanHandler<>(Cart.class), uId, pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateCart(Cart cart) {
        String sql = "update cart set c_count = ?,c_num = ? where c_id = ?";
        try {
            return runner.update(sql, cart.getC_count(), cart.getC_num(), cart.getC_id());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int createCart(Cart cart) {
        String sql = "insert into cart(c_id,u_id,p_id,c_count,c_num) values(?,?,?,?,?)";
        try {
            return runner.update(sql, 0, cart.getU_id(), cart.getP_id(), cart.getC_count(), cart.getC_num());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Cart> getCarts(int uid) {
        String sql = "SELECT * FROM cart c INNER JOIN product p ON c.p_id = p.p_id WHERE c.u_id = ?";
        try {
            List<Map<String, Object>> list = runner.query(sql, new MapListHandler(), uid);
            List<Cart> carts = new ArrayList<>();
            for (Map<String, Object> map : list) {
                Product product = new Product();
                BeanUtils.populate(product, map); // map注入到商品实体

                Cart cart = new Cart();
                BeanUtils.populate(cart, map);  // map注入到购物车实体

                cart.setProduct(product);  // 购物车中包含商品实体
                carts.add(cart);  // 将购物车对象进行存储
            }
            return carts;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    @Override
    public int clearCart(int uid) {
        String sql = "delete from cart where u_id = ?";
        try {
            return runner.update(sql, uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteCart(String cid) {
        String sql = "delete from cart where c_id = ?";
        try {
            return runner.update(sql, cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteCartByUid(String id) {
        String sql = "DELETE from `cart` WHERE u_id = ?";
        try {
            return runner.update(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
